
pkgname=systemd
pkgver=239
pkgrel=1
epoch=1
pkgdesc="System and Session Manager"
arch=('x86_64')
url="http://www.freedesktop.org/wiki/Software/systemd"
license=('GPL2' 'LGPL2.1')
depends=('acl' 'bash' 'glibc' 'kmod' 'hwids' 'util-linux' 'kbd' 'libcap' 'libxslt' 'iptables'
         'linux-api-headers' 'pam' 'xz' 'libgcrypt' 'dbus' 'libidn' 'lz4' 'python2-systemd'
         'quota' 'libseccomp' 'cryptsetup') # 'audit') 
makedepends=('docbook-xsl' 'gperf' 'intltool' 'libmicrohttpd' 'gnu-efi-libs' 'meson' 'ninja')
optdepends=('libmicrohttpd: systemd-journal-gatewayd')
backup=(etc/dbus-1/system.d/org.freedesktop.systemd1.conf
        etc/dbus-1/system.d/org.freedesktop.hostname1.conf
        etc/dbus-1/system.d/org.freedesktop.login1.conf
        etc/dbus-1/system.d/org.freedesktop.locale1.conf
        etc/dbus-1/system.d/org.freedesktop.machine1.conf
        etc/dbus-1/system.d/org.freedesktop.timedate1.conf
        etc/systemd/bootchart.conf
        etc/systemd/system.conf
        etc/systemd/user.conf
        etc/systemd/logind.conf
        etc/systemd/journald.conf
        etc/udev/udev.conf)
conflicts=('gummiboot')
replaces=('gummiboot')
provides=("udev=${pkgver}" 'gummiboot')
groups=('base')
install="systemd.install"
options=('!libtool')
source=("https://github.com/systemd/systemd/archive/v${pkgver}.tar.gz"
        'initcpio-hook-udev'
        'initcpio-install-systemd'
        'initcpio-install-udev'
        'loader.conf'
        'KaOS.conf'
        'KaOS_fallback.conf'
        'splash.bmp'
        'systemd-tmpfiles.hook'
        "https://github.com/systemd/systemd/pull/8580/commits/4af61b251099b1fed4c816a464f8068972a7145c.diff")
md5sums=('6137e3f50390391cf34521d071a1a078'
         'b2da235893d76ed6b810fa5e8d2afe67'
         'f422035c2a36b4fa51f4a369eccc01f4'
         'bde43090d4ac0ef048e3eaee8202a407'
         '30b74b38eaf1178bbc540bda9a24bfe2'
         'eb444ae327472b98a65631ede777720a'
         'b8d8ebeab17edbe91209e705cfc9e774'
         'd7eec59a2aaa54770d22ccfa52d0ac2b'
         '7d5910d12fdfce56228a1aac8c3d618c'
         'b56e36075c5f1ad2de6bf4f6d5c06e0d')

prepare() {
  cd ${pkgname}-${pkgver}
  #rm src/journal/audit_type-to-name.h
  #rm src/udev/keyboard-keys-from-name.gperf
  
  sed -i 's|Linux Boot Manager|Systemd Boot Manager|' src/boot/bootctl.c
  # fix for util-linux 2.32
  #patch -p1 -i ${srcdir}/4af61b251099b1fed4c816a464f8068972a7145c.diff
}

build() {
  mkdir -p build
  cd build
  
  NTP_SERVERS="0.north-america.pool.ntp.org 1.north-america.pool.ntp.org 2.north-america.pool.ntp.org 3.north-america.pool.ntp.org"
  # cp locale-archive from /usr/lib/locale
  export LANG=en_US.UTF-8
  
  meson ../${pkgname}-${pkgver} \
    --prefix=/usr \
    --buildtype=release \
    --libexecdir=/usr/lib \
    -Dsplit-usr=true \
    -Dsplit-bin=true \
    -Drootprefix=/usr \
    -Dlz4=true \
    -Defi=true \
    -Dgnu-efi=true \
    -Daudit=false \
    -Dima=false \
    -Dnetworkd=false \
    -Ddefault-dnssec=no \
    -Ddefault-kill-user-processes=false \
    -Dntp-servers="${NTP_SERVERS}" \
    -Ddefault-hierarchy=hybrid \
    -Dfallback-hostname=KaOS \
    -Ddbuspolicydir=/etc/dbus-1/system.d 
    ninja
}

check() {
  cd build
  ninja test || true
}

package() {
  cd build
  DESTDIR=${pkgdir} ninja install
  
  cd ..
  
  install -dm755 ${pkgdir}/bin
  ln -s ../usr/lib/systemd/systemd ${pkgdir}/bin/systemd

  install -dm755 ${pkgdir}/sbin
  for tool in runlevel reboot shutdown poweroff halt telinit; do
    ln -s '/usr/bin/systemctl' ${pkgdir}/sbin/$tool
  done

  ln -s '../usr/lib/systemd/systemd' ${pkgdir}/sbin/init

  install -d ${pkgdir}/sbin
  ln -s ../usr/bin/udevadm ${pkgdir}/sbin/udevadm

  ln -s ../lib/systemd/systemd-udevd ${pkgdir}/usr/bin/udevd

  install -m644 systemd-${pkgver}/tmpfiles.d/legacy.conf ${pkgdir}/usr/lib/tmpfiles.d

  # Replace in optical 
  sed -i 's#GROUP="dialout"#GROUP="uucp"#g;
          s#GROUP="tape"#GROUP="storage"#g;
          s#GROUP="cdrom"#GROUP="optical"#g' ${pkgdir}/usr/lib/udev/rules.d/*.rules
  sed -i 's/dialout/uucp/g;
          s/tape/storage/g;
          s/cdrom/optical/g' ${pkgdir}/usr/lib/sysusers.d/basic.conf
          
  # add mkinitcpio hooks
  install -Dm644 ${srcdir}/initcpio-install-systemd ${pkgdir}/usr/lib/initcpio/install/systemd
  install -Dm644 ${srcdir}/initcpio-install-udev ${pkgdir}/usr/lib/initcpio/install/udev
  install -Dm644 ${srcdir}/initcpio-hook-udev ${pkgdir}/usr/lib/initcpio/hooks/udev
  
  rm -r ${pkgdir}/usr/lib/rpm
  
  # 208 changed permissions for /var/log/journal
  #install -o root -g systemd-journal -dm2755 ${pkgdir}/var/log/journal
  chown root:systemd-journal ${pkgdir}/var/log/journal
  chmod 2755 ${pkgdir}/var/log/journal{,/remote}
  
  # Make journalctl persistent, set max
  sed -i -e 's|#SystemMaxUse=|SystemMaxUse=25M|' ${pkgdir}/etc/systemd/journald.conf
  
  # Install systemd-boot files, ex-gummi
  install -D -m0644 ${srcdir}/loader.conf ${pkgdir}/usr/lib/systemd/boot/loader/loader.conf
  install -D -m0644 ${srcdir}/KaOS.conf ${pkgdir}/usr/lib/systemd/boot/loader/entries/KaOS.conf
  install -D -m0644 ${srcdir}/KaOS_fallback.conf ${pkgdir}/usr/lib/systemd/boot/loader/entries/KaOS_fallback.conf
  install -D -m0644 ${srcdir}/splash.bmp ${pkgdir}/usr/lib/systemd/boot/splash.bmp
  
  # adjust default PAM config
  sed 's|account required pam_unix.so|account  include system-login|g' -i ${pkgdir}/etc/pam.d/systemd-user
  sed 's|session optional pam_systemd.so|session  include system-login|g' -i ${pkgdir}/etc/pam.d/systemd-user
  
  # pacman hook
  install -Dm644 ../systemd-tmpfiles.hook ${pkgdir}/usr/share/libalpm/hooks/systemd-tmpfiles.hook
  
  # Systemd 239 started shipping openresolv replacement, remove for now, conflicting file only
  rm ${pkgdir}/usr/sbin/resolvconf
}


